//
//  TripDetailCtrl.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/20.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "TripDetailCtrl.h"
#import "MineDataTool.h"
#import "TripDetailDayItem.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "TripDetailScenicCell.h"
#import "TripDetailListItem.h"
#import "TripDetailHotelCell.h"
#import "TripDetailHeaderView.h"
#import "PlanCtrl.h"
#import "DaysRouteInfo.h"
#import "ItemRouteInfo.h"
#import "CreatRouteInfo.h"
/**
 *  行程详情页面
 */
@interface TripDetailCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *changeTripBtn;
@property (nonatomic, strong) TripOrderResult *result;
@property (nonatomic, strong) NSArray *daysArray;
@property (nonatomic, strong) TripDetailHeaderView *headerView;

@end

@implementation TripDetailCtrl

-(TripDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [TripDetailHeaderView headerView];
        [_headerView setWidth:SCREENW];
        _headerView.height = 91;
        [_headerView setOrderResult:self.result];
    }
    return _headerView;
}

-(instancetype)initWithTripOrderResult:(TripOrderResult *)result{
    self = [super init];
    if (self) {
        self.result = result;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"行程详情"];
    
    self.tableView.estimatedRowHeight = 200;
    [self.tableView registerNib:[UINib nibWithNibName:@"TripDetailHotelCell" bundle:nil] forCellReuseIdentifier:@"TripDetailHotelCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TripDetailScenicCell" bundle:nil] forCellReuseIdentifier:@"TripDetailScenicCell"];
    [self.tableView setBackgroundColor:RGBColor(240, 240, 240)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self.tableView selector:@selector(reloadData) name:kNotiTebleViewReload object:nil];    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView beginUpdates];
    [self.tableView setTableHeaderView:self.headerView];
    [self.tableView endUpdates];
}

-(void)requestData{
    [MineDataTool getTripDetailInfoWithPlanId:[NSString stringWithFormat:@"%ld",self.result.plan_id] success:^(TripDetailResult *result) {
        if (result.isSucc) {
            self.daysArray = result.days;
            for (TripDetailListItem *listItem in self.daysArray) {
                for (int i=0; i<listItem.items.count; i++) {
                    TripDetailDayItem *item = listItem.items[i];
                    if (i<listItem.items.count-1) {
                        item.nextDayItem = listItem.items[i+1];
                    }
                }
            }
            [self.tableView reloadData];
        }else{
            [DialogUtils showTips:result.msg];
        }
    } failure:^(NSError *error) {
        [DialogUtils showTips:@"行程详情加载失败"];
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.daysArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"count=%ld",[self.daysArray[section] items].count);
    return [self.daysArray[section] items].count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TripDetailDayItem *item = [self.daysArray[indexPath.section] items][indexPath.row];
    if (item.channel_id == 72) {
        TripDetailScenicCell *cell = [TripDetailScenicCell cellWithTableView:tableView];
        cell.dayItem = item;
        return cell;
    }else{
        TripDetailHotelCell *cell = [TripDetailHotelCell cellWithTableView:tableView];
        cell.dayItem = item;
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    TripDetailDayItem *item = [self.daysArray[indexPath.section] items][indexPath.row];
    if (item.channel_id == 72) {
        CGFloat result = [tableView fd_heightForCellWithIdentifier:@"TripDetailScenicCell" cacheByIndexPath:indexPath configuration:^(TripDetailScenicCell *cell) {
            cell.dayItem = item;
        }];
        return result;
    }else{
        CGFloat result = [tableView fd_heightForCellWithIdentifier:@"TripDetailHotelCell" cacheByIndexPath:indexPath configuration:^(TripDetailHotelCell *cell) {
            cell.dayItem = item;
        }];
        return result;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    TripDetailListItem *item = self.daysArray[section];
    NSString *string = [NSString stringWithFormat:@"第%ld天：%@",item.day_num,item.name];
    return [string sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(self.tableView.width-16, MAXFLOAT)].height+16;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TripDetailListItem *item = self.daysArray[section];
    UIView *view = [[UIView alloc] init];
    view.width = self.tableView.width;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, view.width-32, 0)];
    label.font = [UIFont boldSystemFontOfSize:16];
    label.numberOfLines = 0;
    label.textColor = [UIColor blackColor];
    label.text = [NSString stringWithFormat:@"第%ld天：%@",item.day_num,item.name];
    label.height = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.width, MAXFLOAT)].height +16;
    view.height = label.height;
    
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(8, 0, view.width-16, label.height)];
    [tempView addSubview:label];
    tempView.backgroundColor = [UIColor whiteColor];
    [view addSubview:tempView];
    return view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableView){
        CGFloat sectionHeaderHeight = 50;
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

- (IBAction)adjustTripBtnClick:(UIButton *)sender {
    CreatRouteInfo *info = [[CreatRouteInfo alloc] init];
    info.plan_start = [TripDetailHeaderView dateString:self.result.plan_start];
    info.plan_id = [NSString stringWithFormat:@"%ld",self.result.plan_id];
    info.plan_price = self.result.price;
    info.plan_name = self.result.plan_name;
    
    
    for (int i=0; i<self.daysArray.count; i++) {
        TripDetailListItem *item = self.daysArray[i];
        DaysRouteInfo *daysRouteInfo = [[DaysRouteInfo alloc] init];
        daysRouteInfo.day_num = item.day_num;
        
        for (int j=0; j<item.items.count; j++) {
            TripDetailDayItem *dayItem = item.items[j];
            ItemRouteInfo *itemRouteInfo = [[ItemRouteInfo alloc] init];
            itemRouteInfo.item_id = [NSString stringWithFormat:@"%ld",dayItem.item_id];
            itemRouteInfo.sort = j;
            itemRouteInfo.itemName = dayItem.name;
            itemRouteInfo.price = dayItem.price;
            [daysRouteInfo.items addObject:itemRouteInfo];
        }
        [info.days addObject:daysRouteInfo];
    }
    PlanCtrl *ctrl = [[PlanCtrl alloc] initWithCreatRouteInfo:info];
    [self.navigationController pushViewController:ctrl animated:YES];
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:kNotiTebleViewReload object:nil];
}

@end
